/**
 * 
 */
package vehicleController;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import validator.Validator;
import vehicleEntity.VehicleRentCostEntity;
import vehicleLogic.SetRateLogic;
import constants.Constants;

/**
 * @author Yokesh Kumar
 *
 */
public class SetRateController implements Initializable{
	
	@FXML
	private ComboBox<String> setRateFor;
	@FXML
	private ComboBox<String> vehicleType;
	@FXML
	private Label vehicleTypeLabel;
	@FXML
	private Label fuelRate;
	@FXML
	private TextField fuelRateField;
	@FXML
	private Label kmRate;
	@FXML
	private TextField kmRateField;
	@FXML
	private Label hourlyRate;
	@FXML
	private TextField hourlyRateField;
	@FXML
	private Label dailyRate;
	@FXML
	private TextField dailyRateField;
	@FXML
	private Label weeklyRate;
	@FXML
	private TextField weeklyRateField;
	@FXML
	private Label kmLimit;
	@FXML
	private TextField kmLimitField;
	@FXML
	private Label hrlyInsRate;
	@FXML
	private TextField hrlyInsRateField;
	@FXML
	private Label dailyInsRate;
	@FXML
	private TextField dailyInsRateField;
	@FXML
	private Label weeklyInsRate;
	@FXML
	private TextField weeklyInsRateField;
	@FXML
	private Button setRateButton;
	@FXML
	private Label message;
	
	private static VehicleRentCostEntity setRate;
	private static Double fuel;
	
	/**
	 * This method gets the vehicletype and based on which values  for the table column are fetched from the set rate logic class 
	 * @param event
	 * @Pre Table values are set based on the result received from set rate logic class
	 * @Post The set table column ids are passed to the set rate logic class 
	 * @return none
	 */
	@FXML
	public void fetchVehicleType(ActionEvent event){
		if(setRateFor.getValue() != null && !setRateFor.getValue().isEmpty()){
			SetRateLogic logic = new SetRateLogic();
			message.setText("");
			message.setTextFill(Color.RED);
			if(setRateFor.getValue().equalsIgnoreCase(Constants.VEHICLE_CLASS_CAR) || 
					setRateFor.getValue().equalsIgnoreCase(Constants.VEHICLE_CLASS_TRUCK)){
				fuelRate.setVisible(false);
				fuelRateField.setVisible(false);
				setRateButton.setVisible(false);
				List<String> vehiceTypeList = logic.fetchVehicleType(setRateFor.getValue());
				vehicleType.getItems().clear();
				vehicleType.getItems().addAll(vehiceTypeList);
				vehicleType.setVisible(true);
				vehicleTypeLabel.setVisible(true);
			}
			else{
				vehicleType.setVisible(false);
				vehicleTypeLabel.setVisible(false);
				kmLimit.setVisible(false);
				kmLimitField.setVisible(false);
				kmRate.setVisible(false);
				kmRateField.setVisible(false);
				hourlyRate.setVisible(false);
				hourlyRateField.setVisible(false);
				hrlyInsRate.setVisible(false);
				hourlyRateField.setVisible(false);
				dailyRate.setVisible(false);
				dailyRateField.setVisible(false);
				dailyInsRate.setVisible(false);
				dailyInsRateField.setVisible(false);
				weeklyRate.setVisible(false);
				weeklyRateField.setVisible(false);
				weeklyInsRate.setVisible(false);
				weeklyInsRateField.setVisible(false);
				fuel = logic.fetchFuelRate();
				fuelRateField.setText(fuel.toString());
				fuelRate.setVisible(true);
				fuelRateField.setVisible(true);
				setRateButton.setVisible(true);
			}
		}
		else{
			message.setText("Please select an option to set rate");
		}
	}
	
	/**
	 * This method fetches the rate from database based on the vehicle type chosen
	 * @param event
	 * @Pre Vehicle type is chosen
	 * @Post table column values are fetched from the set rate logic class by passing vehicle type
	 * @return none
	 */
	@FXML
	public void fetchRates(ActionEvent event){
		if(vehicleType.getValue() != null && !vehicleType.getValue().isEmpty()){
			message.setText("");
			message.setTextFill(Color.RED);
			SetRateLogic logic = new SetRateLogic();
			setRate = logic.fetchVehicleRates(vehicleType.getValue().trim());
			if(setRate != null){
				kmLimitField.setText(setRate.getKmLimit().toString());
				kmRateField.setText(setRate.getKmRate().toString());
				hourlyRateField.setText(setRate.getHourlyRate().toString());
				dailyRateField.setText(setRate.getDailyRate().toString());
				weeklyRateField.setText(setRate.getWeeklyRate().toString());
				dailyInsRateField.setText(setRate.getDailyInsRate().toString());
				hrlyInsRateField.setText(setRate.getHourlyInsRate().toString());
				weeklyInsRateField.setText(setRate.getWeeklyInsRate().toString());	
				kmLimit.setVisible(true);
				kmLimitField.setVisible(true);
				kmRate.setVisible(true);
				kmRateField.setVisible(true);
				hourlyRate.setVisible(true);
				hourlyRateField.setVisible(true);
				hrlyInsRate.setVisible(true);
				hrlyInsRateField.setVisible(true);
				dailyRate.setVisible(true);
				dailyRateField.setVisible(true);
				dailyInsRate.setVisible(true);
				dailyInsRateField.setVisible(true);
				weeklyRate.setVisible(true);
				weeklyRateField.setVisible(true);
				weeklyInsRate.setVisible(true);
				weeklyInsRateField.setVisible(true);
				setRateButton.setVisible(true);
			}
			else{
				message.setText("No rates found for the given Vehicle Type");
			}
		}
	}
	
	/**
	 * This method saves the rate values in the database
	 * @param event
	 * @Pre for which type the values in database has to be modified
	 * @Post the set values are passed to the set rate logic
	 * @return none
	 */
	@FXML
	public void saveRates(ActionEvent event){
		SetRateLogic logic = new SetRateLogic();
		message.setText("");
		message.setTextFill(Color.RED);
		if(setRateFor.getValue().equalsIgnoreCase(Constants.FUEL)){
			if(fuelRateField.getText() != null && !fuelRateField.getText().isEmpty()){
				Double newFuelrate = new Double(fuelRateField.getText().trim());
				System.out.println(newFuelrate);
				System.out.println(fuel);
				if(fuel.equals(newFuelrate)){
					message.setText("No Changes made to Fuel rate");
				}
				else{
					String msg = logic.updateFuelRate(newFuelrate);
					message.setText(msg);
					message.setTextFill(Color.GREEN);
				}
			}
		}
		else{
			if(kmLimitField.getText() != null && !kmLimitField.getText().isEmpty() &&
					kmRateField.getText() != null && !kmRateField.getText().isEmpty() &&
					hourlyRateField.getText() != null && !hourlyRateField.getText().isEmpty() &&
					dailyRateField.getText() != null && !dailyRateField.getText().isEmpty() &&
					weeklyRateField.getText() != null && !weeklyRateField.getText().isEmpty() &&
					hrlyInsRateField.getText() != null && !hrlyInsRateField.getText().isEmpty() &&
					dailyInsRateField.getText() != null && !dailyInsRateField.getText().isEmpty() &&
					weeklyInsRateField.getText() != null && !weeklyInsRateField.getText().isEmpty()){
				
				if(setRate.getKmLimit().equals(new Integer(kmLimitField.getText().trim())) && 
						setRate.getHourlyRate().equals(new Double(hourlyRateField.getText().trim())) &&
						setRate.getDailyRate().equals(new Double(dailyRateField.getText().trim())) &&
						setRate.getKmRate().equals(new Double(kmRateField.getText().trim())) &&
						setRate.getWeeklyRate().equals(new Double(weeklyRateField.getText().trim())) &&
						setRate.getHourlyInsRate().equals(new Double(hrlyInsRateField.getText().trim())) &&
						setRate.getDailyInsRate().equals(new Double(dailyInsRateField.getText().trim())) &&
						setRate.getWeeklyInsRate().equals(new Double(weeklyInsRateField.getText().trim())) ){
					
					message.setText("No Changes made to values");
				}
				else{
					if(!Validator.validateStringContainsInteger(kmLimitField.getText().trim())){
						message.setText("Please enter a valid KM Limit");
					}
					else if(!Validator.validateStringContainsDouble(hourlyRateField.getText().trim())){
						message.setText("Please enter a valid Amount for Hourly Rate");
					}
					else if(!Validator.validateStringContainsDouble(dailyRateField.getText().trim())){
						message.setText("Please enter a valid Amount for Daily Rate");
					}
					else if(!Validator.validateStringContainsDouble(kmRateField.getText().trim())){
						message.setText("Please enter a valid Amount for KM Rate");
					}
					else if(!Validator.validateStringContainsDouble(weeklyRateField.getText().trim())){
						message.setText("Please enter a valid Amount for Weekly Rate");
					}
					else if(!Validator.validateStringContainsDouble(hrlyInsRateField.getText().trim())){
						message.setText("Please enter a valid Amount for Hourly Insurance Rate");
					}
					else if(!Validator.validateStringContainsDouble(dailyInsRateField.getText().trim())){
						message.setText("Please enter a valid Amount for Daily Insurance Rate");
					}
					else if(!Validator.validateStringContainsDouble(weeklyInsRateField.getText().trim())){
						message.setText("Please enter a valid Amount for Weekly Insurance Rate");
					}
					else{
						setRate.setKmLimit(new Integer(kmLimitField.getText().trim())); 
						setRate.setHourlyRate(Validator.formatDouble((new Double(hourlyRateField.getText().trim()))));
						setRate.setDailyRate(Validator.formatDouble(new Double(dailyRateField.getText().trim())));
						setRate.setKmRate(Validator.formatDouble(new Double(kmRateField.getText().trim())));
						setRate.setWeeklyRate(Validator.formatDouble((new Double(weeklyRateField.getText().trim()))));
						setRate.setHourlyInsRate(Validator.formatDouble((new Double(hrlyInsRateField.getText().trim()))));
						setRate.setDailyInsRate(Validator.formatDouble((new Double(dailyInsRateField.getText().trim()))));
						setRate.setWeeklyInsRate(Validator.formatDouble((new Double(weeklyInsRateField.getText().trim()))));
						String msg = logic.updateVehicleRates(setRate);
						message.setText(msg);
						message.setTextFill(Color.GREEN);
					}
					
				}
			}
			else{
				message.setText("All values are mandatory");
			}
		}
	}

	/* (non-Javadoc)
	 * @see javafx.fxml.Initializable#initialize(java.net.URL, java.util.ResourceBundle)
	 */
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		Validator.addTextLimiter(kmRateField, 4);
		Validator.addTextLimiter(hourlyRateField, 5);
		Validator.addTextLimiter(dailyRateField, 5);
		Validator.addTextLimiter(weeklyRateField, 6);
		Validator.addTextLimiter(kmLimitField, 5);
		Validator.addTextLimiter(hrlyInsRateField, 5);
		Validator.addTextLimiter(dailyInsRateField, 5);
		Validator.addTextLimiter(weeklyInsRateField, 5);
	}
	
}
